package br.com.sgo.controller;

import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.sgo.model.User;
import br.com.sgo.service.LoginService;

@Resource
public class IndexController {

	private final Result result;
	private final LoginService loginService;

	public IndexController(Result result, LoginService loginService) {
		this.result = result;
		this.loginService = loginService;
	}

	@Path("/")
	public void index() {
		
		if(loginService.hasLoggedUser())
			result.redirectTo(this).home();
	}

	@Path("login")
	public void login(User user) {
		
		try {
			loginService.login(user);
			result.redirectTo(this).home();
		}
		catch(Exception e) {
			result.redirectTo(this).index();
		}
	}
	
	@Path("logout")
	public void logout() {
		
		loginService.logout();
		result.redirectTo(this).index();
	}		
	
	@Path("home")
	public void home() {}	
	
}
